package info.sybian.service;

import info.sybian.model.User;
import objot.util.String2;

import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.Transaction;

public class DoMF extends Do {
	private static final long[] items = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

	@Service
	public String init() throws Exception {
		long begin = System.currentTimeMillis();

		// for(int j = 0; j < 100; j++) {
		boolean ok = false;
		Transaction tx = env.beginTransaction(null, null);
		Database db = env.openDatabase(tx, "sybian", dc);
		try {
			for(int i = 0; i < 10000; i++) {
				User u = new User();
				long id = i;
				u.id = id;
				u.name = "nkadun" + id;
				u.nick = "南开阿当" + id;
				u.pass = "123456";
				u.slotN = 0;
				byte[] key = String2.utf(codec.enc(id, null));
				byte[] data = String2.utf(codec.enc(u, null));
				db.put(tx, new DatabaseEntry(key), new DatabaseEntry(data));
			}
			ok = true;
		}
		finally {
			if(ok)
				tx.commit();
			else
				tx.abort();
		}
		// }

		long end = System.currentTimeMillis();
		System.out.println("Init cost: " + (end - begin));
		return "init";
	}
}
